package com.czy.owner.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.czy.owner.R;
import com.czy.owner.entity.GoodsEntity;
import com.czy.owner.entity.WorkShowEntity;
import com.czy.owner.utils.glide.GlideUtils;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by King on 2017-05-19
 */

public class WorkShowAdapter extends BaseRecyclerAdapter<WorkShowEntity, WorkShowAdapter.WorkShowViewHolder> implements RecyclerHeadersAdapter<RecyclerView.ViewHolder> {
    private WorkGroupOperationListener workGroupOperationListener;

    public WorkShowAdapter(Context context, List<WorkShowEntity> data) {
        super(context, data);
    }

    public WorkShowAdapter(Context context, List<WorkShowEntity> data, boolean slideEnabled, boolean reboundEnabled) {
        super(context, data, slideEnabled, reboundEnabled);
    }

    public void setWorkGroupOperationListener(WorkGroupOperationListener workGroupOperationListener) {
        this.workGroupOperationListener = workGroupOperationListener;
    }

    @Override
    public int getItemViewType(int position) {
        return data.get(position).getShowDataType();
    }

    @Override
    public WorkShowViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            return new WorkShowViewHolder(layoutInflater.inflate(R.layout.item_work_group_recycler, parent, false), viewType);
        } else {
            return new WorkShowViewHolder(layoutInflater.inflate(R.layout.item_work_detail_recycler, parent, false), viewType);
        }
    }


    @Override
    public void onBindViewHolder(WorkShowViewHolder holder, final int position) {
        super.onBindViewHolder(holder, position);
        if (getItemViewType(position) == 0) {
            final WorkShowEntity.SonModelsBean workGroup = (WorkShowEntity.SonModelsBean) data.get(position).getShowData();
            //Log.i("King","onBindViewHolder:" + (data.get(position).getShowData() instanceof WorkShowEntity.SonModelsBean) + "|" + workGroup.isExpanded());
            holder.cbGroupPicked.setChecked(workGroup.isPicked());
            holder.cbGroupPicked.setTag(data.get(position));
            holder.cbGroupPicked.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (workGroupOperationListener != null) {
                        workGroupOperationListener.onChecked(position, data.get(position), v);
                    }
                }
            });
            holder.tvGroupName.setText(workGroup.getModelsName());
            holder.tvGroupName.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (workGroupOperationListener != null) {
                        workGroupOperationListener.onToggleExpanded(position, data.get(position), v);
                    }
                }
            });
        } else {
            final GoodsEntity goodsBean = (GoodsEntity) data.get(position).getShowData();
            //holder.ivDetailLogo.setTag(goodsBean);
            if (TextUtils.isEmpty(goodsBean.getGoodsLogo())) {
                holder.ivDetailLogo.setImageResource(R.mipmap.default_icon);
            } else {
                GlideUtils.loadImage(mContext, goodsBean.getGoodsLogo(), holder.ivDetailLogo, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                    @Override
                    public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {
                    }

                    @Override
                    public void onLoadingError(String source, Exception e) {
                    }
                }, R.mipmap.default_icon, R.mipmap.default_icon);
            }
            holder.tvDetailName.setText(goodsBean.getGoodsName());

            DecimalFormat decimalFormat = new DecimalFormat("0.##");
            holder.tvDetailPrice.setText("￥" + decimalFormat.format(goodsBean.getGoodsSpecValuesPrice()));
            if(goodsBean.getCount() < 1 ){
                goodsBean.setCount(1);
            }
            holder.tvDetailCounter.setText("X " + goodsBean.getCount());
            holder.ivDetailArrow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (workGroupOperationListener != null) {
                        workGroupOperationListener.onSpec(position, data.get(position), v);
                    }
                }
            });
        }
    }

    @Override
    public long getHeaderId(int position) {
        return data.get(position).getModelsId();
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        View view = layoutInflater.inflate(R.layout.item_work_header_recycler, parent, false);
        return new RecyclerView.ViewHolder(view) {
        };
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {
        TextView textView = (TextView) holder.itemView.findViewById(R.id.tv_item_work_header_name);
        String showValue = String.valueOf(data.get(position).getModelsName());
        textView.setText(showValue);
    }

    public class WorkShowViewHolder extends BaseRecyclerAdapter.ViewHolder {
        private int viewType;

        private CheckBox cbGroupPicked;
        private TextView tvGroupName;

        private ImageView ivDetailLogo;
        private TextView tvDetailName;
        private TextView tvDetailPrice;
        private TextView tvDetailCounter;
        private ImageView ivDetailArrow;

        public WorkShowViewHolder(View itemView, int viewType) {
            super(itemView);
            this.viewType = viewType;
            if (viewType == 0) {
                cbGroupPicked = (CheckBox) itemView.findViewById(R.id.cb_item_work_group_picked);
                tvGroupName = (TextView) itemView.findViewById(R.id.tv_item_work_group_name);
            } else {
                ivDetailLogo = (ImageView) itemView.findViewById(R.id.iv_item_work_logo);
                tvDetailName = (TextView) itemView.findViewById(R.id.tv_item_work_name);
                tvDetailPrice = (TextView) itemView.findViewById(R.id.tv_item_work_price);
                tvDetailCounter = (TextView) itemView.findViewById(R.id.tv_item_work_counter);
                ivDetailArrow = (ImageView) itemView.findViewById(R.id.iv_item_work_arrow);
            }
        }
    }

    public interface WorkGroupOperationListener<T> {
        void onToggleExpanded(int position, T t, View view);

        void onChecked(int position, T t, View view);

        void onSpec(int position,T t,View view);
    }
}
